
/* Bring up a window and play with it */
#include "StdAfx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ptgui/pagectl.h>
#include <mmsystem.h>

#define BENCHMARK_SDL

#define NOTICE(X)	printf("%s", X);

#include <SDL/SDL.h>

struct {
	const char *szWindowClass;
	const char *szWindowTitle;
	HINSTANCE hInst;								// current instance
	HWND hWnd;
	WNDPROC SDLProc;
} user_data = {"TestSDL", "SDL Simulate", 0, 0};

Uint32 FastestFlags(Uint32 flags, unsigned int width, unsigned int height,
 unsigned int bpp)
{
  const SDL_VideoInfo *info;

  flags |= SDL_FULLSCREEN;

  info = SDL_GetVideoInfo();
  if ( info->blit_hw_CC && info->blit_fill ) {
    flags |= SDL_HWSURFACE;
  }

  if ( (flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
    if ( info->video_mem*1024 > (height*width*bpp/8) ) {
      flags |= SDL_DOUBLEBUF;
    } else {
      flags &= ~SDL_HWSURFACE;
    }
  }

  return flags;
}

void DrawPict(SDL_Surface *screen, char *bmpfile,
					int speedy, int flip, int nofade)
{
	SDL_Surface *picture;
	SDL_Rect dest, update;

	/* Load the image into a surface */
	if ( bmpfile == NULL ) {
		bmpfile = "sample.bmp";		/* Sample image */
	}
fprintf(stderr, "Loading picture: %s\n", bmpfile);
	picture = SDL_LoadBMP(bmpfile);
	if ( picture == NULL ) {
		fprintf(stderr, "Couldn't load %s: %s\n", bmpfile,
							SDL_GetError());
		return;
	}

	dest.y = 0;
	dest.w = picture->w;
	dest.h = picture->h;
	dest.x = 0;
	update = dest;
	if ( SDL_BlitSurface(picture, NULL, screen, &update) < 0 ) {
		fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
	}
	
	/* Free the picture and return */
	SDL_FreeSurface(picture);
	return;
}

void DrawPict2(SDL_Surface *screen, char *bmpfile,
					int speedy, int flip, int nofade, int i)
{
	SDL_Surface *picture;
	SDL_Rect dest, update;
	int centered;

	/* Load the image into a surface */
	if ( bmpfile == NULL ) {
		bmpfile = "sample.bmp";		/* Sample image */
	}
fprintf(stderr, "Loading picture: %s\n", bmpfile);
	picture = SDL_LoadBMP(bmpfile);
	if ( picture == NULL ) {
		fprintf(stderr, "Couldn't load %s: %s\n", bmpfile,
							SDL_GetError());
		return;
	}
	centered = (screen->w - picture->w)/2;
	if ( centered < 0 ) {
		centered = 0;
	}
	dest.y = (screen->h - picture->h)/2;
	dest.w = picture->w;
	dest.h = picture->h;
NOTICE("testwin: moving image\n");
	dest.x = i;
	update = dest;
	SDL_SetColorKey(picture, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(picture->format, 0, 0, 0));
	if ( SDL_BlitSurface(picture, NULL, screen, &update) < 0 ) {
		fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
	}
	SDL_Flip(screen);
	/* Free the picture and return */
	SDL_FreeSurface(picture);
	return;
}

/* The main Win32 event handler */
LONG CALLBACK SimluateWinMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	if (user_data.SDLProc != NULL)
	{
		return(user_data.SDLProc(hwnd, msg, wParam, lParam));
	}
	else {
		return(DefWindowProc(hwnd, msg, wParam, lParam));
	}
}


ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)SimluateWinMessage;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= 0;
	wcex.hCursor		= 0;
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= 0;
	wcex.lpszClassName	= user_data.szWindowClass;
	wcex.hIconSm		= 0;

	return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   user_data.hInst = hInstance; // Store instance handle in our global variable

   user_data.hWnd = CreateWindow(user_data.szWindowClass, user_data.szWindowTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!user_data.hWnd)
   {
      return FALSE;
   }

   ShowWindow(user_data.hWnd, nCmdShow);
   UpdateWindow(user_data.hWnd);

   return TRUE;
}

rt_uint32_t m_dwTicks, m_nFPS;
rt_uint32_t timer_start(void)
{
	timeBeginPeriod(1);
	m_dwTicks=timeGetTime();
	timeEndPeriod(1);
	return m_dwTicks;
}
rt_uint32_t timer_get_delta(void)
{
	rt_uint32_t dwRet;

	timeBeginPeriod(1);
	if (m_dwTicks==0xFFFFFFFF) m_dwTicks=timeGetTime();
	dwRet=timeGetTime()-m_dwTicks;
	timeEndPeriod(1);

	return dwRet;
}

SDL_Surface *screen;
int speedy, flip, nofade;

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	/* Options */
	int delay;
	int w, h;
	int desired_bpp;
	Uint32 video_flags;
	MSG Msg;

	timer_start();
	/* Set default options and check command-line */
	speedy = 0;
	flip = 0;
	nofade = 0;
	delay = 1;
	w = 800;
	h = 600;
	desired_bpp = 0;
	video_flags = 0;	

	MyRegisterClass(hInstance);
	user_data.SDLProc = NULL;
	// Perform application initialization:
	if (!InitInstance (hInstance, SW_HIDE)) 
	{
		return FALSE;
	}

	if ( SDL_Init(SDL_INIT_VIDEO, &user_data) < 0 ) {
		fprintf(stderr,
			"Couldn't initialize SDL: %s\n", SDL_GetError());
		exit(1);
	}
	atexit(SDL_Quit);			/* Clean up on exit */

	/* Initialize the display */
	screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
	if ( screen == NULL ) {
		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
					w, h, desired_bpp, SDL_GetError());
		exit(1);
	}
	printf("Set %dx%dx%d mode\n",
			screen->w, screen->h, screen->format->BitsPerPixel);
	printf("(video surface located in %s memory)\n",
			(screen->flags&SDL_HWSURFACE) ? "video" : "system");

	ptgui_page_system_init();
	do
	{
		if (SDL_GetAppState() & SDL_APPACTIVE) {
			if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
			{
				TranslateMessage(&Msg);
				DispatchMessage(&Msg);
			}
			else
			{
				static rt_uint32_t fElapsed=0;
				static rt_uint32_t nFrames=0;
				DWORD dwDelta;

				while ((dwDelta=timer_get_delta()) == 0) {
					if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
					{
						TranslateMessage(&Msg);
						DispatchMessage(&Msg);
					}
				}
				timer_start();
				fElapsed+=dwDelta;
				nFrames++;
				if (fElapsed>=1000)
				{
					m_nFPS=nFrames;
					fElapsed=0;
					nFrames=0;
				}

				ptgui_page_controller_do_frame(dwDelta);
				SDL_FillRect(screen, NULL , SDL_MapRGB(screen->format, 255, 255, 255));
				ptgui_page_controller_do_render();
				SDL_Flip(screen);
			}
		}
		else {
			GetMessage(&Msg, NULL, 0, 0);
			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
		
	} while (Msg.message!=WM_QUIT);

	exit(0);
}
